package com.situation.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situation.mapper.BizInStockInfoMapper;
import com.situation.mapper.BizInStockMapper;
import com.situation.mapper.BizProductMapper;
import com.situation.mapper.BizSupplierMapper;
import com.situation.pojo.DO.*;
import com.situation.pojo.VO.InStockInfoPro2VO;
import com.situation.pojo.VO.InStockInfoProVO;
import com.situation.pojo.VO.InStockVO;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * @description:
 * @author: 羽程
 * @create: 2021-12-07 11:23
 **/
@Service
public class InStockServiceImpl {
    @Autowired
    private BizInStockMapper inStockMapper;
    @Autowired
    private BizSupplierMapper supplierMapper;
    @Autowired
    private BizProductMapper productMapper;
    @Autowired
    private BizInStockInfoMapper inStockInfoMapper;

    @Transactional
    public void insert(InStockVO inStockVO) throws IllegalAccessException{
        //随机生成发放单号
        String OUT_STOCK_NUM = UUID.randomUUID().toString();
        int itemNumber = 0;//记录该单的总数
//        获取商品明细
        List<BizInStockInfo> products = inStockVO.getProduct();
        if(!CollectionUtils.isEmpty(products)){
            for (BizInStockInfo product : products) {
                //发放数量
                int productNumber = product.getProductNumber();
                //物资编号,不是主键
                String pNum = product.getPNum();
                BizProduct dbProduct = productMapper.findProBypNum(pNum);
                if (dbProduct == null) {
                    throw new IllegalAccessException("产品不存在");
                }else {
                    BizInStockInfo inStockInfo = new BizInStockInfo();
                    inStockInfo.setCreateTime(LocalDateTime.now());
                    inStockInfo.setModifiedTime(LocalDateTime.now());
                    inStockInfo.setProductNumber(productNumber);
                    inStockInfo.setPNum(pNum);
                    inStockInfo.setInNum(OUT_STOCK_NUM);
                    inStockInfoMapper.insert(inStockInfo);
                    itemNumber+=productNumber;
                }
                BizInStock inStock = new BizInStock();

                inStock.setCreateTime(LocalDateTime.now());
                inStock.setProductNumber(itemNumber);
                String username = (String) SecurityUtils.getSubject().getPrincipal();
                inStock.setOperator(username);
                inStock.setType(inStockVO.getType());
                inStock.setSupplierId(inStockVO.getSupplierId());
                inStock.setRemark(inStockVO.getRemark());
                //生成入库单
                inStock.setInNum(OUT_STOCK_NUM);
                //设置为待审核
                inStock.setStatus(2);
                inStockMapper.insert(inStock);
            }
        }else {
            throw new IllegalAccessException("物资不能为空");
        }
    }

    /**
     * 从回收站中恢复数据
     * @param bizInStock
     * @return
     */
    public int back(BizInStock bizInStock){
        bizInStock.setStatus(0);
        List<BizInStockInfo> pnum = inStockMapper.findPnum(bizInStock.getInNum());
        // 已通过,修改库存


            pnum.forEach(e -> {
                // 申请出库的数量
                Integer number = e.getProductNumber();
                // 库存总数

                Long stock = inStockMapper.findStock(e.getPNum());

                inStockMapper.updateStock(e.getPNum(), stock + number);
            });
        return inStockMapper.back(bizInStock);
    }

    /**
     * 移入回收站
     * @param bizInStock
     * @return
     */
    public int remove(BizInStock bizInStock){
        bizInStock.setStatus(1);
        return inStockMapper.back(bizInStock);
    }

    /**
     * 查询明细
     */
    public PageInfo<InStockInfoProVO> detail2(BizInStock bizInStock,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        BizInStock inStock = inStockMapper.findInStock(bizInStock);

        List<InStockInfoProVO> detail = productMapper.findDetail(inStock.getInNum());

        PageInfo<InStockInfoProVO> pageInfo =new PageInfo<>(detail);
        return pageInfo;
    }

    /**
     * 查询明细头
     */
    public BizSupplier detail1(BizInStock bizInStock){

        BizInStock inStock = inStockMapper.findInStock(bizInStock);
        BizSupplier bizSupplier = new BizSupplier();
        bizSupplier.setId(inStock.getSupplierId());


        BizSupplier supplierById = supplierMapper.findSupplierById(bizSupplier);
        return supplierById;
    }

    /**
     * 通过inNum删除入库表信息
     * @param inNum
     * @return
     */
    @Transactional
    public int delete(String inNum){

        inStockMapper.deleteInStock(inNum);
        inStockInfoMapper.deleteInfo(inNum);

        return 1;
    }


    /**
     * 多条件模糊查询入库信息分页
     * @param inStockVO
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<InStockVO> findInStockList(InStockVO inStockVO,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<InStockVO> inStockList = inStockMapper.findInStockList(inStockVO);
        PageInfo<InStockVO> pageInfo=new PageInfo<>(inStockList);
        return pageInfo;
    }

    /**
     * 入库审核
     * @param bizInStock
     * @return
     */
    public int publish(BizInStock bizInStock){
        bizInStock.setStatus(2);
        return inStockMapper.back(bizInStock);
    }
}
